package com.briup.pts.service.impl;

import com.briup.pts.config.exception.ServiceException;
import com.briup.pts.config.response.ResultCode;
import com.briup.pts.domain.bean.FenceHouse;
import com.briup.pts.domain.vo.FenceHouseWithChildren;
import com.briup.pts.mapper.FenceHouseMapper;
import com.briup.pts.service.FenceHouseService;
import com.briup.pts.util.UuidUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Optional;

/**
 * @author 25095
 * @description
 * @data 2024/11/27-11:19
 */
@Service
@RequiredArgsConstructor
public class FenceHouseServiceImpl implements FenceHouseService {
    private final FenceHouseMapper mapper;

    @Override
    public PageInfo<FenceHouse> pageQueryByCondition(Integer pageNum,
                                                     Integer pageSize, String fhName) {
        try (Page<FenceHouse> ignored = PageHelper.startPage(pageNum, pageSize)) {
            // 根据条件查询数据库中的数据
            List<FenceHouse> categories = mapper.queryPage(fhName);

            // 将查询的结果数据放入到PageInfo中返回
            return new PageInfo<>(categories);
        }
    }

    @Override
    public FenceHouseWithChildren selectById(String fhId) {
        FenceHouseWithChildren fenceHouseWithChildren = mapper.selectById(fhId);
        return Optional.ofNullable(fenceHouseWithChildren).orElseThrow(() -> new
                ServiceException(ResultCode.FH_IS_NOT_EXISTS));
    }

    @Override
    public void saveOrUpdate(FenceHouse fh) {
        if (!StringUtils.hasText(fh.getFhId())) {
            String id = UuidUtil.randomUuid();
            fh.setFhId(id);
            mapper.addsaveOrUpdate(fh);
        } else {
            mapper.updatasaveOrUpdate(fh);
        }
    }

    @Override
    public void deleteById(String hId) {
        mapper.deleteById(hId);
    }

    @Override
    public void deleteByIdAll(List<String> ids) {
        mapper.deleteByIdAll(ids);
    }


}
